package com.mrzhou.log.aspect;

import com.mrzhou.core.tool.utils.ClassUtil;
import com.mrzhou.log.annotation.ApiLog;
import com.mrzhou.web.util.WebArgumentUtil;
import com.mrzhou.web.util.WebUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.annotation.PathVariable;

import java.lang.reflect.Method;
import java.util.Map;

@Aspect
public class ApiLogAspect {

    /**
     * 为可以获取@RequestBody的参数, 这里通过获取controller层的参数获取请求的参数
     * @param point
     * @param apiLog
     * @return
     * @throws Throwable
     */
    @Around("@annotation(apiLog)")
    public Object aroundLog(ProceedingJoinPoint point, ApiLog apiLog) throws Throwable {
        // 获取代理对象的类名称
        String className = point.getTarget().getClass().getName();
        // 获取代理的方法名称
        String methodName = point.getSignature().getName();
        // 获取方法对象
        MethodSignature methodSignature = (MethodSignature) point.getSignature();
        Method targetMethod = methodSignature.getMethod();
        Map<String, Object> params = WebArgumentUtil.handlerArgumentToMap(targetMethod, point);

        // 执行方法
        Object result = point.proceed();

        return result;
    }

}
